package com.yonyou.cyxdms.sysAuth.service.controller.manage.org;

import com.yonyou.cyx.framework.controller.base.BaseController;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.sysAuth.service.dto.OrgDTO;
import com.yonyou.cyxdms.sysAuth.service.dto.area.OrgOutDto;
import com.yonyou.cyxdms.sysAuth.service.service.org.OrgService;
import com.yonyou.cyxdms.sysAuth.service.vo.org.*;
import com.yonyou.cyxdms.sysAuth.service.vo.user.UserVO;
import com.yonyou.cyxdms.sysmanage.client.vo.org.OrgAndParentVO;
import com.yonyou.cyxdms.sysmanage.client.vo.org.OrgParentVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author maxingjian
 * @since 2019-08-26
 */
@Api(value = "组织", tags = {"组织"})
@RestController
@RequestMapping("/org")
public class OrgController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrgController.class);

    @Autowired
    public OrgService orgService;

    /**
     * 通过id获取下级组织
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @GetMapping("/tree")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgVO> getChildOrg(@ApiParam(name = "type", value = "是否查询数据是否有效的：10011001、有效，10011002、全部", required =
            true) @RequestParam String type,
                                   @ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId) {
        return orgService.getChildOrg(type, orgId);
    }

    /**
     * 通过id获取下级组织
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @GetMapping("/trees")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgVO> getChildOrgs() {
        return orgService.getChildOrgs();
    }


    /**
     * 通过id获取明细
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @GetMapping("/detail")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public OrgDetailVO getDetailOrg(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId) {
        return orgService.getOrgDetail(orgId);
    }

    /**
     * 通过上级id获取能查询的品牌
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @GetMapping("/getOrgBrand")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<BrandListVO> getOrgBrand(@ApiParam(name = "orgId", value = "上级组织id", required = true) @RequestParam String orgId) {
        return orgService.getOrgBrand(orgId);
    }

    /**
     * 通过上级id获取查询的车系
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @GetMapping("/getOrgSeries")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<SeriesListVO> getOrgSeries(@ApiParam(name = "orgId", value = "上级组织id", required = true) @RequestParam String orgId,
                                           @ApiParam(name = "brandId", value = "品牌id", required = true) @RequestParam String brandId) {
        return orgService.getOrgSeries(orgId, brandId);
    }

    /**
     * 通过id获取明细
     *
     * @param:
     * @return:
     * @auther: maxingjian
     * @since: 2019/8/15
     */
    @PutMapping("/save")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public OrgDetailVO saveDetailOrg(@RequestBody @Valid OrgDetailVO orgDetailVO) {
        return orgService.saveDetailOrg(orgDetailVO);
    }

    /**
     * 获取登录用户组织信息
     *
     * @auther: 李贺
     * @since: 2019/9/6
     */
    @GetMapping("/getOrg")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public OrgAndParentVO getOrg(@ApiParam(name = "orgId", value = "组织id", required = false) @RequestParam(required = false, defaultValue = "") String orgId) {
        return orgService.getOrg(orgId);
    }

    /**
     * 根据组织id 获得组织信息
     *
     * @auther: 李贺
     * @since: 2019/12/6
     */
    @GetMapping("/getOrgByOrgIds")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgParentVO> getOrgByOrgIds(@RequestParam("orgIds") String orgIds) {
        if (StringUtils.isNullOrEmpty(orgIds)) {
            return null;
        }
        return orgService.getOrgByOrgIds(orgIds);
    }

    /**
     * 获取组织信息列表
     *
     * @param:
     * @return:
     * @auther: tanjie
     * @since: 2019/9/19
     */
    @GetMapping("getOrgList")
    @ApiOperation("获取组织信息列表")
    public List<OrgDTO> getOrgList() {
        return orgService.getOrgList();
    }


    /**
     * 根据组织类型获取组织
     *
     * @param:
     * @return:
     * @auther: MAXINGJIAN
     * @since: 2019/9/25
     */
    @GetMapping("/getOrgByOrgType")
    @ApiOperation("根据组织类型获取组织")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgByTypeVO> getOrgByOrgType(@ApiParam(name = "orgType", value = "组织类型", required = true) @RequestParam(required = true) String orgType,
                                             @ApiParam(name = "companyId", value = "公司id", required = false) @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                                             @ApiParam(name = "appId", value = "appId", required = false) @RequestParam(value = "orgId", required = false, defaultValue = "") String appId) {
        return orgService.getOrgByOrgType(orgType, companyId, appId);
    }

    /**
     * 根据组织类型获取组织
     *
     * @param:
     * @return:
     * @auther: MAXINGJIAN
     * @since: 2019/9/25
     */
    @GetMapping("/getOrgByOrgType4cms")
    @ApiOperation("根据组织类型获取组织")
    @ApiImplicitParams({@ApiImplicitParam(value = "用户id", name = "userId", required = true, dataType = "string",
            paramType = "header")})
    public List<OrgByTypeVO> getOrgByOrgType4cms(@ApiParam(name = "orgType", value = "组织类型", required = true) @RequestParam(required = true) String orgType,
                                             @ApiParam(name = "companyId", value = "公司id", required = false) @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                                             @ApiParam(name = "appId", value = "appId", required = false) @RequestParam(value = "orgId", required = false, defaultValue = "") String appId,
                                                 @ApiParam(name = "parentOrgId", value = "parentOrgId", required = false) @RequestParam(value = "parentOrgId", required = false, defaultValue = "") String parentOrgId,
                                                 @ApiParam(name = "orgCode", value = "orgCode", required = false) @RequestParam(value = "orgCode", required = false, defaultValue = "") String orgCode,
                                                 @ApiParam(name = "orgName", value = "orgName", required = false) @RequestParam(value = "orgId", required = false, defaultValue = "") String orgName) {
        return orgService.getOrgByOrgType4cms(orgType, companyId, appId,parentOrgId,orgCode,orgName);
    }

    /**
     * 获得厂端部门信息
     *
     * @auther: 李贺
     * @since: 2019/10/14
     */
    @GetMapping("gms/getOrg")
    @ApiOperation("获得厂端部门信息")
    public GmsOrgVO getGmsOrg(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId", required = true) Long orgId) {
        return orgService.getGmsOrg(orgId);
    }


    /**
     * @param: Map<id, ownercode>
     * @return: Map<id, DealerOrganizationDTO>
     * @auther: tanjie
     * @since: 2019/10/30
     */
    @PostMapping("getOrgNameByOwnerCode/{appId}")
    @ApiOperation("获取组织名称")
    public Map<Long, OrgDealerVO> getOrgNameByOwnerCode(@RequestBody OrgNameDealerVO orgNameDealerVO) {
        return this.orgService.getOrgNameByOwnerCode(orgNameDealerVO.getAppId(), orgNameDealerVO.getDealerCodes());
    }

    /**
     * 保存品牌
     *
     * @return
     * @author 李贺
     * @since 2019/10/31
     */
    @PutMapping("addDealerOrgRelation")
    @ApiOperation("保存品牌")
    public void insertDealerOrgRelation(@RequestBody OrgDetailVO orgDetailVO, @RequestParam("id") Integer id) {
        orgService.insertDealerOrgRelation(orgDetailVO, id);
    }

    /**
     * 删除品牌
     *
     * @return
     * @author 李贺
     * @since 2019/10/31
     */
    @DeleteMapping("delDealerOrgRelation")
    @ApiOperation("保存品牌")
    public void delDealerOrgRelation(@RequestParam("orgRelationId") Long orgRelationId) {
        orgService.delDealerOrgRelation(orgRelationId);
    }

    /**
     * 获取所有销售部门
     *
     * @return
     * @author 李贺
     * @since 2019/11/20
     */
    @GetMapping("/getSaleOrg")
    @ApiOperation("获取所有销售部门")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgParentVO> getSaleOrg(@RequestParam(value = "orgId", required = false) Long orgId) {
        return orgService.getSaleOrg(orgId);
    }

    /**
     * 获取所有事业部
     *
     * @return
     * @author 李贺
     * @since 2019/11/25
     */
    @GetMapping("/getBusinessOrg")
    @ApiOperation("获取所有事业部")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgParentVO> getBusinessOrg() {
        return orgService.getBusinessOrg();
    }

    /**
     * 获取所有部门
     *
     * @return
     * @author wst
     * @since 2021/11/25
     */
    @GetMapping("/getBusinessOrgAll")
    @ApiOperation("获取所有事业部")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgParentVO> getBusinessOrgAll() {
        return orgService.getBusinessOrgAll();
    }

    /**
     * 获取所有部门
     *
     * @return
     * @author wst
     * @since 2021/11/25
     */
    @GetMapping("/getBusinessOrgAllByAppId")
    @ApiOperation("获取所有事业部")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgParentVO> getBusinessOrgAllByAppId(@RequestParam("appId") String appId) {
        return orgService.getBusinessOrgAllByAppId(appId);
    }



    /**
     * 通过orgID获得所有人
     *
     * @return
     * @author 李贺
     * @since 2019/11/25
     */
    @GetMapping("/getUserByOrgId")
    @ApiOperation("通过orgID获得所有人")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<UserVO> getUserByOrgId(@RequestParam("orgId") Long orgId) {
        return orgService.getUserByOrgId(orgId);
    }

    /**
     * 通过上级orgID和下属组织类型获得下属组织
     *
     * @return
     * @author 李贺
     * @since 2019/12/13
     */
    @GetMapping("/org/{parentOrgId}/{orgType}")
    @ApiOperation("通过上级orgID和下属组织类型获得下属组织")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgVO> getOrgByTypeAndParent(@PathVariable("parentOrgId")Long parentOrgId,@PathVariable("orgType")Long orgType) {
        return orgService.getOrgByTypeAndParent(parentOrgId, orgType);
    }

    /**
     * 获得销售小组
     *
     * @return
     * @author 李贺
     * @since 2020/1/7
     */
    @GetMapping("/sales/team")
    @ApiOperation("获得销售小组")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "userId", name = "userId", dataType = "string", paramType = "header")
    })
    public List<OrgByTypeVO> listSalesTeam() {
        return orgService.listSalesTeam();
    }

    @GetMapping("/all/out")
    public List<OrgOutDto> getOrgOut(){
        return orgService.getOrgAllOut();
    }
}